package com.yyq.statistics.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.result.R;
import com.yyq.statistics.client.UserClient;
import com.yyq.statistics.entity.Daily;
import com.yyq.statistics.service.DailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author yyq
 * @since 2021-05-08
 */
@CrossOrigin
@RestController
@RequestMapping("/statistics/daily")
public class DailyController {
    @Autowired
    private UserClient userClient;
    @Autowired
    private DailyService dailyService;
    //获取统计数据
    @GetMapping("getStatisticsData/{day}")
    public R getStatisticsData(@PathVariable String day){
        int registerNum = userClient.getRegisterNum(day);
        QueryWrapper<Daily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        Daily one = dailyService.getOne(wrapper);
        //如果存在 先删除在添加
        if(one!=null){
            dailyService.removeById(one.getId());
        }
        //添加数据
        Daily daily=new Daily();
        //获取统计信息
        //Integer registerNum = (Integer) ucenterClient.registerCount(day).getData().get("countRegister");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //创建统计对象
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        dailyService.save(daily);



        return R.ok();
    }

    //2　返回统计数据
    @GetMapping("getChartData/{types}/{begin}/{end}")
    public R getChartData(@PathVariable String types,@PathVariable String begin,@PathVariable String end){
        Map<String,Object> map =dailyService.getChartData(types,begin,end);
        return R.ok().data(map);
    }




}

